﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BbsbWebService.Classes;
using BbsbWebService.Enums;

namespace BbsbWebService.Repositories
{
    public abstract class PowerRepositoryBase<T> : RepositoryBase<T> where T : CommandItem
    {
        public abstract PowerState Toggle(Guid id);

        protected abstract PowerState SendPowerBase(CommandItem item, PowerOption option);

        public PowerState SendPower(Guid id, PowerOption option)
        {
            PowerState retVal = PowerState.Unknown;
            try
            {
                if (HasItem(id))
                {
                    retVal = SendPowerBase(GetItem(id), option);
                }
            }
            catch (Exception ex)
            {
                Logging.Logger.Instance.LogException(ex, "Error sending power option '{0}' on item '{1}'", option, id);
            }

            return retVal;
        }

        public virtual void Refresh()
        {
            // Do nothing really.
        }

        public virtual void UpdateState(Guid id, PowerState state)
        {
            if (HasItem(id))
            {
                var item = GetItem(id);
                item.State = state;
            }
        }
    }
}
